package org.apache.oozie.tools.workflowgenerator.client.property.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.oozie.tools.workflowgenerator.client.property.Property;
import org.apache.oozie.tools.workflowgenerator.client.property.PropertyTable;
import org.apache.oozie.tools.workflowgenerator.client.widget.NodeWidget;

import com.google.gwt.xml.client.Text;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Element;

public class SubWFPropertyTable extends PropertyTable {

    protected Grid grid;
    protected List<Property> configs;
    protected TextBox apppath;
    protected RadioButton rby;
    protected RadioButton rbn;

    public SubWFPropertyTable(NodeWidget w) {
        super(w);
        initConf();
        initWidget();
    }

    /**
     * Initialize configuration
     */
    protected void initConf() {
        configs = new ArrayList<Property>();
        configs.add(new Property("mapred.job.queue.name", ""));

        widgetList = new ArrayList<NodeWidget>();
        updateWidgetList();
        widgetDropDown = new ArrayList<NodeWidget>();
        updateWidgetDropDown();
    }

    /**
     * Generate xml elements of subworkflow action and attach them to xml doc
     */
    public void generateXML(Document doc, Element root, NodeWidget next) {

        Element action = doc.createElement("action");
        action.setAttribute("name", current.getName());

        // create <sub-workflow>
        Element subwfele = doc.createElement("sub-workflow");
        action.appendChild(subwfele);

        // create <app-path>
        Element appele = doc.createElement("app-path");
        Text t = doc.createTextNode(apppath.getText());
        appele.appendChild(t);
        subwfele.appendChild(appele);

        // create <propagate-configuration>
        if (rby.getValue()) {
            Element proele = doc.createElement("propagate-configuration");
            subwfele.appendChild(proele);
        }

        // create <configuration>
        configToXML(configs, subwfele, doc);

        Element okele = doc.createElement("ok");
        okele.setAttribute("to", next.getName());
        action.appendChild(okele);

        Element errele = doc.createElement("error");
        NodeWidget kill = getKillNode();
        errele.setAttribute("to", kill == null ? "" : kill.getName());
        action.appendChild(errele);

        root.appendChild(action);
    }

    /**
     * Initialize widgets shown in property table
     */
    protected void initWidget() {

        grid = new Grid(6, 2);
        this.add(grid);

        this.setAlwaysShowScrollBars(true);
        this.setSize("100%", "80%");

        // insert row for Name
        grid.setWidget(0, 0, createLabel("Name"));
        name = new TextBox();
        name.setText("SubWF"); // default value
        grid.setWidget(0, 1, formatCell(name));

        // insert row for OK
        grid.setWidget(1, 0, createLabel("OK"));
        okVal = new ListBox();
        createWidgetDropDownList(okVal);
        grid.setWidget(1, 1, formatCell(okVal));

        // insert row for ERROR
        grid.setWidget(2, 0, createLabel("Error"));
        errorVal = new ListBox();
        updateErrorDropDownList();
        grid.setWidget(2, 1, formatCell(errorVal));

        // insert row for App path
        grid.setWidget(3, 0, createLabel("App Path"));
        apppath = new TextBox();
        grid.setWidget(3, 1, formatCell(apppath));

        // insert row for Propagate config
        grid.setWidget(4, 0, createLabel("Propagate Config"));
        HorizontalPanel radiopanel = new HorizontalPanel();
        radiopanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_BOTTOM);
        rbn = new RadioButton("propagateGroup", "false");
        rby = new RadioButton("propagateGroup", "true");
        rbn.setChecked(true);
        radiopanel.add(rby);
        radiopanel.add(rbn);
        grid.setWidget(4, 1, radiopanel);

        // insert row for Configuration
        grid.setWidget(5, 0, createLabel("Configuration"));
        grid.setWidget(5, 1, createSubTable("Property Name", "Value", configs, null));

    }

}
